<?php

namespace CloudTowerDev\CloudTowerIDCP;

use YunTaIDC\Plugin\PluginBase;

use YunTaIDC\Events\CreateServiceEvent;
use YunTaIDC\Events\RenewServiceEvent;
use YunTaIDC\Events\DeleteServiceEvent;
use YunTaIDC\Events\ProductConfigEvent;
use YunTaIDC\Events\LoginServiceEvent;

class CloudTowerIDCP extends PluginBase{

    private function SendData($array, $url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($array));
        $result = curl_exec($curl);
        // $this->getSystem()->getLogger()->addSystemLog('测试：'.$result);
        curl_close($curl);
        return json_decode($result, true);
    }

    public function CreateService(CreateServiceEvent $event){
        $Configs = $event->getProduct()->getConfigOption();
        $Service = $event->getService();
        $Server = $event->getProduct()->getServer();
        $postdata = array(
            'ctuser' => $Server->getServerUsername(),
            'ctpass' => $Server->getServerPassword(),
            'product' => $Configs['product'],
            'period' => $Service->getPeriod()['remark'],
            'username' => $Service->getUsername(),
            'password' => $Service->getPassword(),
        );
        $url = $Server->getServerDomain() .'index.php?p=Api&a=createService';
        $result = $this->SendData($postdata, $url);
        if($result['status'] == 'success'){
            return true;
        }else{
            $this->getSystem()->getLogger()->addSystemLog('云塔插件对接返回：'.$result['code']);
            return false;
        }
    }

    public function RenewService(RenewServiceEvent $event){
        $Period = $event->getPeriod();
        $Service = $event->getService();
        $Server = $event->getProduct()->getServer();
        $postdata = array(
            'ctuser' => $Server->getServerUsername(),
            'ctpass' => $Server->getServerPassword(),
            'period' => $Period['remark'],
            'service' => $Service->getUsername(),
        );
        $url = $Server->getServerDomain() .'/index.php?p=Api&a=renewService';
        $result = $this->SendData($postdata, $url);
        if($result['status'] == 'success'){
            return true;
        }else{
            $this->getSystem()->getLogger()->addSystemLog('云塔对接插件返回：'.$result['code']);
            return false;
        }
    }

    public function DeleteService(DeleteServiceEvent $event){
        return true;
    }

    public function LoginService(LoginServiceEvent $event){
        $url = $event->getServer()->getServerDomain().'index.php?p=Api&a=loginService';
        echo '<form action="'.$url.'" method="POST"><input type="hidden" name="username" value="'.$event->getService()->getUsername().'"><input type="hidden" name="password" value="'.$event->getService()->getPassword().'"><button type="submit">点击跳转</button></form>';
    }

    public function ProductConfig(ProductConfigEvent $event){
        return array(
            'product' => array('label' => '对接产品ID', 'type' => 'number', 'placeholder'=>'对接产品ID'),
        );
    }

}

?>